/**
 * Created by Lcp on 2018/6/2.
 */
/**
 * Created by Lcp on 2017/12/20.
 */
var h = $(window).height(),
    addList = [],//添加指令
    afkssj = '',
    afjssj = '',//案发起止时间
    flag = true;//条件关闭开启判断
$(function(){
    $('.row-hoof').css({height: h, backgroundColor: '#d5dbe7',overflow: 'hidden'});
    $('.containers').css({height: h - 20 + 'px'});
    $('.noContent').css({height: h - 330 + 'px'});
    //日期
    laydate.render({
        elem: '#afsj',
        type: 'datetime',
        range: '~',
        theme: 'molv',//主题
        min: '1900-1-1 00:00:00',
        max: 1,
        format: 'yyyy-MM-dd HH:mm:ss',
        done: function(value, date, endDate){
            if(value != ''){
                afkssj = value.split('~')[0];
                afjssj = value.split('~')[1];
            }else{
                afkssj = '';
                afjssj = '';
            }
        }
    });
    //取消默认右键事件
    document.oncontextmenu = function(){
        return false;
    }
   judge(1);
    $(window).resize(function(){
        h = $(window).height();
        $('.row-hoof').css({height: h, backgroundColor: '#d5dbe7',overflow: 'hidden'});
        $('.containers').css({height: h - 20 + 'px'});
        $('.noContent').css({height: h - 300 + 'px'});
        judge(1);
    })
//获取指令列表
    $.ajax({
        type: 'get',
        url: baseUrls + 'commandStore/list',
        data: {userid: $('#userId').val()},
        beforeSend: function(){
            Loading();
        },
        success: function(response){
            closeLoading();
            if(response.responseCode == '1000'){
                if(response.content.length > 9){
                    $('.num').text("+9");
                } else{
                    $('.num').text(response.content.length);
                }
                if($('.num').text() == 0){
                    $('.num').text('');
                }
                for(var i = 0; i <response.content.length; i++){
                    addList.push(response.content[i].infonum);
                    $('.ul-contents').append('<li><span class="firstSpan">'+ response.content[i].infonum +'</span><span class="secondSpan">'+ response.content[i].infoname +'</span><span onclick="cleared(this)"><i class=" fa fa-minus" aria-hidden="true"></i></span></li>')
                }
            } else{
                //   Msg('案件类型获取失败');
            }
        },
        error: function(){
            closeLoading();
            //  Msg('案件类型获取失败');
        }
    })
})
function nofind(item){
    item.src = 'common/img/nothing.png';
    item.onerror = null;
}

function showDetail(uniq){
    layer.open({
        type: 2,
        area: ['90%', h * 0.9 + 'px'],
        fixed: false, //不固定
        maxmin: false,
        content: 'layer/layer.html?' + uniq + '&' + $('#userId').val()
    });

}
function nulls(item){
    var callback;
    if(item != null && item != undefined){
        callback = item;
    } else {
        callback = '无';
    }
    return callback;
}
//关闭搜索条件
function allClose(item){
    if(flag){
        $('.contentList').css({borderLeft: 'none'});
        $('#mation').slideUp();
        $(item).html('<span>全部展起</span><img src="thread/img/right.png" onerror="nofind(this)">');
        flag = false;
    }else{
        $('.contentList').css({borderLeft: '5px solid #3b5998'});
        $('#mation').slideDown();
        $(item).html('<span>全部收起</span><img src="thread/img/down.png" onerror="nofind(this)">');
        flag = true;
    }
    judge(1);
}
//研判
function judge(pageIndex){
    $.ajax({
        type: 'get',
        url: baseUrls + 'homepage/glaj',
        data: {
            cjsjbegin: afkssj,
            cjsjend: afjssj,
            shr: $.trim($('.shr').val()),
            cjr: $.trim($('.cjr').val()),
            shzt: $('.caseType').val(),
            page: pageIndex,
            size: 4
        },
        beforeSend: function(){
            Loading();
        },
        success: function(response){
            closeLoading();
            if(response.responseCode == '1000'){
                if(response.content.list.length > 0){
                    $('#list').html('');
                    $('.content').show();
                    $('.noContent').hide();
                    $('.total').html('串并列表共<b>'+ response.content.total +'</b>条');
                    response.content.list.forEach(function(item,index){
                        var status = '',//说明
                            classStyle = '',//类名
                            statusDescription = '',//理由
                            Msg = '',//信息
                            ajmcs = item.ajmcs != null ? item.ajmcs.split('~') : '',//相似案件名称
                            span = '';//相似案件信息
                        item.ajbhs.split(',').forEach(function(items,indexs){
                            span += '<span class="ajbh"  onclick="showDetail(\''+ items +'\')">'+ items +'&nbsp&nbsp'+ nulls(ajmcs[indexs]) +'</span>';
                        });
                        if(item.shzt == '2'){
                            status = '<span class=" statusLists newstatusListAgreen">通过</span>';;
                            statusDescription = '';
                        }else{
                            status = '<span class=" statusLists newstatusListAgreen">驳回</span>';
                            if(item.shyj != null){
                                statusDescription = nulls(item.shyj);
                            }else{
                                statusDescription = '';
                            }
                        }
                        if(index % 2 == 0){
                            classStyle = 'liBg';
                        }
                        Msg = '<span class="ajbh">审核时间： '+ nulls(item.shsj) +'；创建人：'+ nulls(item.ccjr) +'；创建时间：'+ nulls(item.cjsj) +'；</span>';
                        $('#list').append('<li class="'+ classStyle +'"><div class="nums">'+ (index + 1) +'</div><div class="left"><div class="leftTop">'+ status +'</div><div class="leftBottom">'+ statusDescription +'</div></div>'+
                            '<div class="right">'+
                            '<div class="rightTop commonRight">'+
                            '<div class="topLeft">串并案名称</div>'+
                            '<div class="topRight">'+
                            '<div class="case">'+
                            '<span class="ajbh">'+ nulls(item.glbh) +'</span>'+
                            '<span class="ajmc">'+ nulls(item.glmc) +'</span>'+
                            '</div>'+
                            '</div>'+
                            '</div>'+
                            '<div class="rightTop commonRight"><div class="topLeft">原案件编号</div><div class="topRight"><div class="case"><span class="ajbh"  onclick="showDetail(\''+ item.ajbh +'\')"  onmouseup="shopping(this)" id="'+ index +'"  onmouseenter="tips(\'鼠标右键添加至指令\','+ index +')">'+ nulls(item.ajbh) +'&nbsp&nbsp'+ nulls(item.ajmc) +'</span><span class="ajmc"></span><input type="hidden" class="ajbhInput" value="'+ (item.ajbh)+ ',' + item.ajbhs +'"><input type="hidden" class="ajmcInput" value="'+ (item.ajmc)+ ',' + item.ajmcs +'"></div></div></div>'+
                            '<div class="rightBottom commonRight">'+
                            '<div class="bottomLeft">相似案件编号</div>'+
                            '<div class="topRight">'+
                            '<div class="case">'+ span +'</div>'+
                            '</div>'+
                            '</div>'+
                            '<div class="rightBottom commonRight">'+
                            '<div class="bottomLeft">信息</div>'+
                            '<div class="topRight">'+
                            '<div class="case">'+ Msg +'</div>'+
                            '</div>'+
                            '</div>'+
                            '</div>'+
                            '</li>');
                    })
                    $('#demo_pag1').bs_pagination({
                        totalPages: response.content.pages,
                        visiblePageLinks: 8,
                        currentPage: response.content.pageNum,
                        showGoToPage: false,
                        showRowsPerPage: false,
                        showRowsInfo: false,
                        showRowsDefaultInfo: false,
                        navListContainerClass: 'col-xs-12',
                        onChangePage: function (event, data) {
                            judge(data.currentPage);
                        }
                    });
                }else{
                    $('#list').html('');
                    $('#demo_pag1').html('');
                    $('.content').hide();
                    $('.noContent').show();
                    Msg('暂无数据');
                    Echarts();
                }
            }else{
                Msg();
            }
        },
        error: function(){
            closeLoading();
            Msg();
        }
    })
}

//echarts
function Echarts(){
    var noContent = echarts.init(document.getElementById('noContent'));
    function createRandomItemStyle() {
        return {
            normal: {
                color: 'rgb(' + [
                    Math.round(Math.random() * 160),
                    Math.round(Math.random() * 160),
                    Math.round(Math.random() * 160)
                ].join(',') + ')'
            }
        };
    }
    var option = {
        tooltip: {
            show: false
        },
        series: [{
            name: '',
            type: 'wordCloud',
            size: ['80%', '80%'],
            textRotation : [0, 45, 90, -45],
            textPadding: 0,
            autoSize: {
                enable: true,
                minSize: 14
            },
            data: [
                {
                    name: "诈骗案",
                    value: 10000,
                    itemStyle: {
                        normal: {
                            color: 'black'
                        }
                    }
                },
                {
                    name: "相似度",
                    value: 6181,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "线索统计",
                    value: 4386,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "手动研判",
                    value: 4055,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "常住人口",
                    value: 2467,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "抢夺案",
                    value: 2244,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "其他",
                    value: 1898,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "简要案情",
                    value: 1484,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "相似案件",
                    value: 1112,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "火车站",
                    value: 965,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "相似案件",
                    value: 847,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "其他",
                    value: 582,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "抢劫案",
                    value: 555,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "抢盗",
                    value: 550,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "吸毒",
                    value: 462,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "强奸",
                    value: 366,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "诈骗",
                    value: 360,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "火车站",
                    value: 282,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "相似度",
                    value: 273,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "火车站",
                    value: 265,
                    itemStyle: createRandomItemStyle()
                }
            ]
        }]
    };
    noContent.setOption(option);
}


//指令

//右键添加指令
function shopping(item,evnet){
    console.log($(item).parent().find('.ajbhInput').val().split(','));
    var event = event ? event : window.event;
    if(event.which == 3){
        var ajbh = $(item).parent().find('.ajbhInput').val().split(','),
            ajmc = $(item).parent().find('.ajmcInput').val().split(','),
            flag = true;
        for(var i = 0; i <= ajbh.length; i++){
            if(addList.indexOf(ajbh[i]) >= 0){
                flag = false;
                break;
            }
        }
        if(flag == false){
            Msg('您已添加');
        } else {
            var kind = 0,
                str = $(item).html();
            if(str.indexOf('A') >= 0){
                kind = 0;
            }
            if(str.indexOf('S') >= 0){
                kind = 1;
            }
            $.ajax({
                type: 'post',
                url: baseUrls + 'commandStore/add',
                data: {
                    userid: $('#userId').val(),
                    infonum: $(item).parent().find('.ajbhInput').val(),
                    infoname: $(item).parent().find('.ajmcInput').val(),
                    cstype: kind
                },
                beforeSend: function(){
                    Loading();
                },
                success: function(response){
                    closeLoading();
                    if(response.responseCode == '1000'){
                        var flyElm = $(item).clone().css('opacity', '0.8');
                        flyElm.css({
                            zIndex: 9000,
                            display: 'block',
                            position: 'absolute',
                            top: $(item).offset().top + 'px',
                            left: $(item).offset().left + 'px',
                            width: $(item).width() + 'px',
                            height: $(item).height() + 'px',
                            color: 'transparent',
                            background: 'rgba(40,56,82,.5)',
                            '-webkit-box-shadow': '0 0 5px #0090e1 inset',
                            '-moz-box-shadow': '0 0 5px #0090e1 inset',
                            '-ms-box-shadow': '0 0 5px #0090e1 inset',
                            '-o-box-shadow': '0 0 5px #0090e1 inset',
                            'box-shadow': '0 0 5px #0090e1 inset'
                        });
                        $('body').append(flyElm);
                        flyElm.animate({
                                top: $('.shopping').offset().top,
                                left: $('.shopping').offset().left,
                                width: 2,
                                height: 2
                            },
                            500,
                            function(){
                            for(var i = 0; i < ajbh.length;i++){
                                addList.push(ajbh[i]);
                                $('.ul-contents').append('<li><span class="firstSpan">'+ ajbh[i] +'</span><span class="secondSpan">'+ ajmc[i] +'</span><span onclick="cleared(this)"><i class=" fa fa-minus" aria-hidden="true"></i></span></li>');
                            }
                                if(addList.length > 9){
                                    $('.num').text("+9");
                                } else{
                                    $('.num').text(addList.length);
                                }
                                flyElm.remove();
                            })
                    } else {
                        closeLoading();
                        Msg('指令添加失败');
                    }
                },
                error: function(){
                    closeLoading();
                    Msg('指令添加失败');
                }
            })
        }
    }
}
function showList(){
    $('#remark').css('display','block');
    $('.shoppingList').animate({
        width: '80%',
        height: '500px',
        marginTop: '-250px'
    },500);
    setTimeout(function(){
        $('#remark .button').css('display','block');
    },500)
}
function hideList(){
    $('#remark .button').css('display','none');
    $('.shoppingList').animate({
        width: 0,
        height: 0,
        marginTop: 0
    },500);
    setTimeout(function(){
        $('#remark').css('display','none');
    },500)
}
function stopP(event){
    var e = arguments.callee.caller.arguments[0]||event;
    if (e && e.stopPropagation) {
        e.stopPropagation();
    } else if (window.event) {
        window.event.cancelBubble = true;
    }
}
function  cleared(item) {
    $.ajax({
        type: 'post',
        url: baseUrls + 'commandStore/delete',
        data: {
            userid: $('#userId').val(),
            infonum: $(item).parent().children('.firstSpan').html()
        },
        beforeSend: function(){
            Loading();
        },
        success: function(response){
            closeLoading();
            if(response.responseCode == '1000'){
                addList.splice($(item).parent().index(),1);
                $(item).parent().remove();
                if(addList.length > 0){
                    $('.num').text(addList.length);
                }else{
                    $('.num').text('');
                }
            }else{
                Msg('删除失败');
            }
        },
        error: function(){
            closeLoading();
            Msg('删除失败');
        }
    })
}

